//
//  OrderTicketController.m
//  WuJieLive
//
//  Created by iMac on 2017/12/28.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "OrderTicketController.h"
#import "OrderTicketModel.h"
#import "OrderTicketCell.h"

@interface OrderTicketController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView * tableview;
@property (nonatomic , strong) NSMutableArray * dataArray;
@property (nonatomic , strong) UIButton * currentBtn;

@end

@implementation OrderTicketController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setNavi
{
    self.title = @"优惠券";
}

- (void)setUI
{
    UIButton * btn = [[UIButton alloc]init];
    [btn setBackgroundColor:[UIColor whiteColor]];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 20;
    [btn setTitle:@"不使用优惠券" forState:(UIControlStateNormal)];
    [btn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    btn.titleLabel.font = FontSize(15);
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    [view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.top.offset(10);
        make.height.offset(40);
    }];
    [btn addTarget:self action:@selector(donotUse) forControlEvents:(UIControlEventTouchUpInside)];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 60 ) style:(UITableViewStyleGrouped)];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.tableview registerNib:[UINib nibWithNibName:@"OrderTicketCell" bundle:nil] forCellReuseIdentifier:@"OrderTicketCell"];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.rowHeight = 110;
    [self.view addSubview:self.tableview];
    self.tableview.tableHeaderView = view;
    
}

- (void)setData
{
    self.dataArray = [NSMutableArray array];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"goods_redis_key":self.goods_redis_key};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@order/pre-order-coupon",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            for (int i = 0 ; i < [responseObject[@"data"] count]; i ++) {
                OrderTicketModel * model = [[OrderTicketModel alloc]init];
                [model setValuesForKeysWithDictionary:responseObject[@"data"][i]];
                [self.dataArray addObject:model];
            }
            [self.tableview reloadData];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderTicketCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    self.currentBtn.selected = NO;
    self.currentBtn = cell.useBtn;
    self.currentBtn.selected = YES;
    OrderTicketModel * model = self.dataArray[indexPath.section];
    if (self.block) {
        self.block(model.coupon_user_id);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderTicketCell * cell = [tableView dequeueReusableCellWithIdentifier:@"OrderTicketCell" forIndexPath:indexPath];
    [cell bindDataWithModel:self.dataArray[indexPath.section]];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)donotUse
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
